<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/9
 * Time: 10:27
 */

namespace Futures\Controller;
use Think\Controller;
class CouponController extends BaseController{

    public function index(){
        $cid=I('get.cid');
        $Model=M('futures_voucher');
        $where['id']=$cid;
        $couponData=$Model->where($where)->find();
        if($couponData['end_time']<time()||$couponData['status']==0){
            $this->error('对不起,优惠券不可用');
        }
        $user = cookie('user_auth');
        $uid=$user['uid'];
        $userData = M('futures_voucher_log')->where(array('user_id'=>$uid,'voucher_id'=>$couponData['id']))->select();
        $count = count($userData);
        $this->assign('count',$count);
        $this->assign('couponData',$couponData);
        $this->assign('cid',$cid);
        $this->display();
    }
//优惠券分享的方法
    public function receive(){
        header('Content-Type:application/json; charset=utf-8');
        //调用上面方法获取openid
        $user = cookie('user_auth');
        $openid = $user['openid'];
        //获取代金卷ID和用户ID
        $cid=I('post.cid');
        $uid=$user['uid'];
        //获取优惠券信息
        $Model=M('futures_voucher');
        $where['id']= $cid;
        $couponData=$Model->where($where)->find();
        //判断代金卷是否过期，是否正在进行
        if($couponData['end_time']<time()||$couponData['status']==0){
            $backArr['state']=1;
            $backArr['msg']='对不起,优惠券不可用';
            exit(json_encode($backArr));
        }
        //获取用户领取记录
        $Records=M('futures_voucher_records');
        $res=$Records->where(array('user_id'=>$uid,'voucher_id'=>$couponData['id']))->find();
        $log = M('futures_voucher_log')->where(array('user_id'=>$uid,'voucher_id'=>$couponData['id']))->select();
        $count = count($log);
        //判断该用户是否有领取这张代金卷
        if(!empty($log)){//用户有领取
        	if($count >= $couponData['person_limit']){
        		$backArr['state']=1;
        		$backArr['msg']='代金卷每人限领'.$couponData['person_limit'].'张';
        		exit(json_encode($backArr));
        	}else{
        		$backArr['state']=0;
        		$backArr['msg']='领取成功';
        		$Data['create_time'] = time();
        		$Records->where('id='.$res['id'])->setInc('count');		//增加领取数量
        		$Records->where('id='.$res['id'])->save($Data);			//修改领取时间
        		$Data=array(
        				'user_id'=>$uid,							//用户ID
        				'voucher_id'=>$couponData['id'],			//代金卷ID
        				'money'=>$couponData['money'],				//代金卷金额
        				'count'=> "1",								//领取数量
        				'type' => "1",								//代金卷使用情况（未使用）
        				'note' => '领取代金卷',							//领取说明
        				'create_time'=>time(),						//领取时间
        		);
        		M('futures_voucher_log')->add($Data);		//该代金卷的使用和领取记录
        		exit(json_encode($backArr));
        	}
        }else{//用户未领取
            $Data=array(
                'user_id'=>$uid,							//用户ID
                'voucher_id'=>$couponData['id'],			//代金卷ID
                'money'=>$couponData['money'],				//代金卷金额
                'count'=>"1",								//领取数量
                'overdue_time'=>$couponData['end_time'],	//代金卷结束时间
                'create_time'=>time(),						//领取时间
            );
            $result=$Records->add($Data);		//该用户的领取记录
            $Data['type'] = 1;					//代金卷使用情况（未使用）
            $Data['note'] = '领取代金卷';			//领取说明
            $Data['use_time'] = 0;				//领取说明
            M('futures_voucher_log')->add($Data);		//该代金卷的使用和领取记录
            $backArr['state']=0;
            $backArr['msg']='领取成功';
            exit(json_encode($backArr));
        }
    }

}

